// pages/travelAgency/addPartnerDriver.js
var Tool = require("../../utils/ztool.js");
var tool = new Tool();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchstr: '',
    list: [],
    sijiid: ''

  },

  inputchange: function (e) {
    var str = e.detail.value
    this.setData({
      searchstr: str
    })
  },

  searchClick: function () {
    var str = this.data.searchstr
    console.log(str)

    if (str == '') {
      this.showError('请输入搜索关键字')
      return
    }

    search(this)
  },

  callPhone: function (e) {
    if (e.currentTarget.id == "") {
      wx.showToast({
        title: '号码为空',
        icon: 'none',
      });
      return;
    }
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.id //仅为示例，并非真实的电话号码
    });
  },

  pullto: function (e) {
    var that = this
    wx: wx.showModal({
      title: '确定添加？',
      content: '确定将这个司机添加到合作团队吗？',
      showCancel: true,
      cancelText: '取消',
      confirmText: '确定',
      success: function (res) {
        if (res.confirm) {
          that.data.sijiid = e.currentTarget.id
          addPartner(that)
        } else {

        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })

    // this.setData({
    //   sijiid: e.currentTarget.id
    // });

  },

  //提示toast
  showError: function (msg) {
    var that = this;
    that.setData({
      formError: true,
      errorMessage: msg
    });

    setTimeout(function () {
      that.setData({
        formError: false,
        errorMessage: '未知错误'
      });
    }, 2000);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },



  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

})

function search(that) {
  that.data.list = []
  var userInfo = wx.getStorageSync('userInfo');
  wx.showToast({
    title: '正在加载...',
    icon: 'loading',
    duration: 10000
  });
  wx.request({

    url: 'https://www.lanshuicar.com/Server/CooPeratePartners.ashx',
    data: {
      PageSize: '50',
      PageIndex: '1',
      ID: userInfo.ID,
      CType: '3',
      Info: '1',
      Conditions: that.data.searchstr
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      if (res.statusCode == 404) {
        //接口加载失败
        console.log('加载接口错误');
        //隐藏Toast
        wx.hideToast();
        //显示错误信息
        that.showError('接口请求失败，请重试')
        return
      }
      // success
      var data = tool.getRegularJson(res.data);
      var code = data.code;
      console.log(res);
      if (code == 0) {
        wx.showToast({
          title: '搜索成功',
        });
        that.setData({
          list: that.data.list.concat(data.data),
        });
      } else {

      }
    },
    fail: function () {

    },
    complete: function () {
      // complete
      // wx.hideToast();
      // wx.stopPullDownRefresh();
    }
  })
}

function addPartner(that) {
  var userInfo = wx.getStorageSync('userInfo');
  wx.showToast({
    title: '正在加载...',
    icon: 'loading',
    duration: 10000
  });
  wx.request({

    url: 'https://www.lanshuicar.com/Server/AgencyServer/AgencyAddCoopPartners.ashx',
    data: {
      ID: userInfo.ID,
      PID: that.data.sijiid,
      Type: '1',
      OpType: '0'
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      if (res.statusCode == 404) {
        //接口加载失败
        console.log('加载接口错误');
        //隐藏Toast
        wx.hideToast();
        //显示错误信息
        that.showError('接口请求失败，请重试')
        return
      }
      // success
      var data = tool.getRegularJson(res.data);
      var code = data.code;
      console.log(res);
      if (code == 0) {
        wx.showToast({
          title: '拉入团队成功',
        });
        wx.navigateBack()
        // that.setData({
        // list: that.data.list.concat(data.data),
        // });
      } else if (code == -2) {
        wx.showToast({
          title: '该司机已经是该团队司机',
          icon: 'none',
          duration: 3000
        });
      } else {
        wx.showToast({
          title: '其他错误',
          icon: 'none',
          duration: 3000
        });
      }
    },
    fail: function () {

    },
    complete: function () {
      // complete
      // wx.hideToast();
      // wx.stopPullDownRefresh();
    }
  })
}